package com.itheima.case2.web;


import com.itheima.case2.pojo.vo.*;
import com.itheima.case2.service.impl.UserServiceImpl;
import com.itheima.case2.utils.BaseController;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
    /user/add -> 调用UserServlet的add方法
    /user/deleteBatch -> 调用UserServlet的deleteBatch方法
            -> 为springMVC的请求分发铺垫
 */
@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    private void deleteBatch(HttpServletRequest request, HttpServletResponse response) throws IOException {

    }
   /* @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求和响应编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //1.获取方法名
        String url = request.getRequestURI();
        int lastIndex = url.lastIndexOf('/');
        String method = url.substring(lastIndex + 1, url.length());

        if (method.equals("find")) {
            //分页查询功能
//            this.find(request, response);
            find(request, response);
        }else  if (method.equals("add")) {
            //添加功能
            add(request, response);
        }else  if (method.equals("update")) {
            //修改功能
            update(request, response);
        }else  if (method.equals("delete")) {
            //删除功能
            delete(request, response);
        }

    }*/
    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //接收请求参数
            String uid = request.getParameter("uid");

            UserServiceImpl service = new UserServiceImpl();
            service.deleteUser(uid);

            BaseController.printResult(response,new Result(true,"删除成功!"));

        } catch (IOException e) {
            BaseController.printResult(response,new Result(false,"删除失败!"));
        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //接收请求参数
            UpdateUser uu = BaseController.parseJSON2Object(request, UpdateUser.class);

            UserServiceImpl service = new UserServiceImpl();
            service.updateUser(uu);

            BaseController.printResult(response,new Result(true,"修改成功!"));

        } catch (IOException e) {
            BaseController.printResult(response,new Result(false,"修改失败!"));
        }

    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //接收请求参数
            AddUser au = BaseController.parseJSON2Object(request, AddUser.class);

            UserServiceImpl service = new UserServiceImpl();
            service.addUser(au);

            BaseController.printResult(response,new Result(true,"添加成功!"));

        } catch (IOException e) {
            BaseController.printResult(response,new Result(false,"添加失败!"));
        }

    }

    private void find(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数
        try{
            QueryPageBean queryPageBean = BaseController.parseJSON2Object(request, QueryPageBean.class);

            //调用业务层的查询方法
            UserServiceImpl service = new UserServiceImpl();
            PageResult page = service.selectByPage(queryPageBean);
//            int i = 1/0;
            //响应给客户端
            BaseController.printResult(response,new Result(true,"查询成功!",page));
        } catch (Exception e) {
            e.printStackTrace();
            BaseController.printResult(response, new Result(false, "查询失败!"));
        }
    }


}
